package de.duester.genealogy.adapter;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import de.duester.genealogy.R;
import de.duester.genealogy.model.Date;
import de.duester.genealogy.model.Event;
import de.duester.genealogy.model.Person;
import de.duester.genealogy.model.Place;
import de.duester.genealogy.model.service.EventService;
import de.duester.genealogy.model.service.PersonService;
import de.duester.genealogy.model.service.PlaceService;

public class PersonListAdapter extends ArrayAdapter<Person> implements Comparator<Person> {
	private Activity activity;
	private List<Person> persons;

	public PersonListAdapter(Context context, int textViewResourceId, List<Person> persons) {
		super(context, textViewResourceId, persons);
		activity = (Activity) context;
		this.persons = persons;
		Collections.sort(this.persons, this);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v;

		if (convertView == null) {
			LayoutInflater li = activity.getLayoutInflater();
			v = li.inflate(R.layout.person_list_info, null);
		} else
			v = convertView;

		TextView tvName = (TextView) v.findViewById(R.id.tvPerson);
		TextView tvDates = (TextView) v.findViewById(R.id.tvDates);
		TextView tvResidence = (TextView) v.findViewById(R.id.tvResidence);

		Person p = persons.get(position);

		tvName.setText(p.toString());

		String s = "";
		List<Event> birthEvents = PersonService.getEvents(p, "birth", "child");
		if (birthEvents.size() > 0) {
			Date birthDate = EventService.getDate(birthEvents.get(0), "date");
			s = birthDate.toString();
		}
		if (!PersonService.isAlive(p)) {
			Date deathDate = EventService.getDate(PersonService.getEvents(p, "death")
					.get(0), "date");
			s += " - " + deathDate.toString();
		}
		tvDates.setText(s);

		List<Event> moves = PersonService.getEvents(p, "move");
		Place place = null;
		if (moves.size() == 0) {
			// Geburtsort nehmen
			if (birthEvents.size() > 0)
				place = EventService.getPlace(birthEvents.get(0), "place");
		} else
			// letzten Ort nehmen
			place = EventService.getPlace(moves.get(moves.size() - 1), "placeAfter");
		if (place != null)
			tvResidence.setText(PlaceService.getName(place.getNameCode()));

		switch (p.getGender()) {
		case Person.MALE:
			v.setBackgroundResource(R.drawable.person_list_info_male);
			break;
		case Person.FEMALE:
			v.setBackgroundResource(R.drawable.person_list_info_female);
			break;
		default:
			break;
		}

		return v;
	}

	@Override
	public Person getItem(int position) {
		return persons.get(position);
	}

	@Override
	public int compare(Person lhs, Person rhs) {
		return lhs.compare(rhs);
	}

}
